

package com.wake.widget.widget.dialog;

import android.content.Context;
import android.util.AttributeSet;

import com.wake.widget.R;
import com.wake.widget.layout.NMUIPriorityLinearLayout;
import com.wake.widget.skin.NMUISkinHelper;
import com.wake.widget.skin.NMUISkinValueBuilder;
import com.wake.widget.util.NMUIResHelper;

public class NMUIBottomSheetRootLayout extends NMUIPriorityLinearLayout {

    private final int mUsePercentMinHeight;
    private final float mHeightPercent;
    private final int mMaxWidth;

    public NMUIBottomSheetRootLayout(Context context) {
        this(context, null);
    }

    public NMUIBottomSheetRootLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        setBackground(NMUIResHelper.getAttrDrawable(context, R.attr.nmui_skin_support_bottom_sheet_bg));
        NMUISkinValueBuilder builder = NMUISkinValueBuilder.acquire();
        builder.background(R.attr.nmui_skin_support_bottom_sheet_bg);
        NMUISkinHelper.setSkinValue(this, builder);
        builder.release();

        int radius = NMUIResHelper.getAttrDimen(context, R.attr.nmui_bottom_sheet_radius);
        if (radius > 0) {
            setRadius(radius, HIDE_RADIUS_SIDE_BOTTOM);
        }
        mUsePercentMinHeight = NMUIResHelper.getAttrDimen(context, R.attr.nmui_bottom_sheet_use_percent_min_height);
        mHeightPercent = NMUIResHelper.getAttrFloatValue(context, R.attr.nmui_bottom_sheet_height_percent);
        mMaxWidth = NMUIResHelper.getAttrDimen(context, R.attr.nmui_bottom_sheet_max_width);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthSize > mMaxWidth) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode);
        }
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightSize >= mUsePercentMinHeight) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    (int) (heightSize * mHeightPercent), MeasureSpec.AT_MOST);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
